// pages/shop/erji//fapiao/fapiaoEdit.js
var base = require('../../../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const { getUserToken, getUserInfo } = require('../../../../lib/user.js')
const { URL } = require('../../../../lib/urls.js')
const { HTTP } = require('../../../../lib/http.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    companyName:"",
    shuihao:"",
    address:"",
    tel:"",
    bank:"",
    bankAccount:"",
    type:1,
    fapiaoModel:null,
    fpTypes: ["普票 VAT（Normal）","专票 VAT（Special）"],
    showFPXZ:false,
    invoiceCode:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _t: base._t(), //翻译
    });
    let that = this
    console.log("options+++++++++++", options)
    var item = JSON.parse(decodeURIComponent(options.data));
    this.setData({
      fapiaoModel: item,
      companyName: item.CompanyName,
      shuihao: item.CompanyTax,
      address: item.CompanyAddress,
      tel: item.Mobile,
      bank: item.BankName,
      bankAccount: item.BankAccount,
      type: item.TickectType,
      invoiceCode: item.InvoiceCode||""
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //内容编辑
  bindNameTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      companyName: name
    })
  },
  //内容编辑
  bindShuiHaoTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      shuihao: name
    })
  },
  //内容编辑
  bindAddressTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      address: name
    })
  },
  //内容编辑
  bindTelTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      tel: name
    })
  },
  //内容编辑
  bindBankTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      bank: name
    })
  },
  //内容编辑
  bindBankAccTag: function (e) {
    let name = e.detail.value;
    console.log(name)
    this.setData({
      bankAccount: name
    })
  },
  //配送时间编辑
  changFpType: function () {
    this.setData({ showFPXZ: true });
  },
  // 弹出框控制
  onClose() {
    this.setData({
      showFPXZ: false,
    });
  },
  // 到店人数确认
  onConfirm(event) {
    const { picker, value, index } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      type: index+1,
      showFPXZ: false,
    });
  },

  //保存发票
  submitTap: function () {
    if (this.data.companyName.length<1){
      wx.showToast({
        title: "请输入公司名称",
        icon: 'none'
      })
      return;
    }
    if (this.data.shuihao.length < 1) {
      wx.showToast({
        title: "请输入税号",
        icon: 'none'
      })
      return;
    }
    if (this.data.type!=1){
      if (this.data.address.length < 1) {
        wx.showToast({
          title: "请输入地址",
          icon: 'none'
        })
        return;
      }
      if (this.data.bank.length < 1) {
        wx.showToast({
          title: "请输入银行",
          icon: 'none'
        })
        return;
      }
      if (this.data.bankAccount.length < 1) {
        wx.showToast({
          title: "请输入银行账号",
          icon: 'none'
        })
        return;
      }
      
    }
    wx.showLoading({
      title: '正在处理...'
    })
    HTTP({
      url: URL.fapiao.addInvoice,
      userToken: getUserToken(),
      data: {
        InvoiceCode: this.data.invoiceCode,
        CompanyName: this.data.companyName,
        CompanyTax: this.data.shuihao,
        TickectType: this.data.type,
        CompanyAddress: this.data.address,
        BankName: this.data.bank,
        BankAccount: this.data.bankAccount,
        Mobile: this.data.tel,
        MemberId: getUserInfo().memberId,
      },
      success: (res) => {
        wx.showToast({
          title: '发票保存成功',
        })
        var pages = getCurrentPages();//当前页面栈
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
          beforePage.getNetDataList();//触发父页面中的方法
        }
        setTimeout(function () {
          //你需要执行的代码
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
})